import time
import string
import re
import os

class CVersionFile:
    '''version file base class'''
    name = ''
    def __init__(self, path):
        self.path = path

    def SetConfig(self, config):
        self.config = config

    def GetFileName(self):
        return os.path.join(self.path, self.name)

    def Change(self):
        pass

class CVfSgold(CVersionFile):
    '''Sgold version file: sib.c'''

    name = "sib.c"
    def __init__(self, path):
        CVersionFile.__init__(self, path)


    def Change(self):
        f = file(self.path+os.sep+CVfSgold.name)
        tmpf = file(self.path+os.sep+"bak_"+CVfSgold.name, 'w')
        for line in f:
            #change version high
            if re.match("#define.*HIGH.*\"$",line) :
                line=re.sub(r'".*"', r'"%s"'% self.config.sgold_ver_high, line)

            #change version low
            if re.match("#define.*LOW.*\"$",line) :
                line=re.sub(r'".*"', r'"%s"'% self.config.sgold_ver_low,line)

            #change version type
            if re.search(r'"[RID]"',line) :
                line=re.sub(r'"[RID]"', r'"%s"'% self.config.sgold_ver_type,line)

            #change build date
            if re.search(r'sw_date ',line) :
                line=re.sub(r'".*"', r'"%s"' % time.strftime('%d%m%Y'),line)

            #change model year
            if re.search(r'model_year',line) :
                line=re.sub(r'".*"', r'"%s"' % time.strftime('%Y'),line)

            #change SVN
            if re.search(r'^ .*VER_sw_version_svn.*;$',line) :
                line=re.sub(r'".*"', r'"%s"' % self.config.sgold_ver_svn,line)
    
            tmpf.write(line)
        f.close()
        tmpf.close()

        #write the new content to original file
        f = file(self.path+os.sep+CVfSgold.name,'w')
        tmpf = file(self.path+os.sep+"bak_"+CVfSgold.name)
        data=tmpf.read()
        f.write(data)

        #close all file
        f.close()
        tmpf.close()
        os.remove(self.path+os.sep+"bak_"+CVfSgold.name)

class CVfS12(CVersionFile):
    '''S12 version file: version.h'''

    name = "version.h"
    def __init__(self, path, type):
        CVersionFile.__init__(self, path)
        self.type=type

    def Change(self):

        origFilePN=self.path+os.sep+CVfS12.name
        tmpFilePN=self.path+os.sep+"bak_"+CVfS12.name
        f = file(origFilePN)
        tmpf = file(tmpFilePN, 'w+')


        dcm_type = self.type
        if dcm_type=="L42L" :
            dcm_type="NCIC"

        #this while is just used to change sgold version section
        while True:
            line = f.readline()
            if len(line)==0 :
                break

            #change sgold version 
            if re.search("NISSAN_DCM_"+dcm_type,line) :
                tmpf.write(line)
                break

            tmpf.write(line)
        #end of while      

        #read the rest data
        restData = f.read()
        restData = re.sub(r"#define SGOLD_VER_HIGH.*0x[\dA-Fa-f]+", "#define SGOLD_VER_HIGH 0x%02x" % string.atoi(self.config.sgold_ver_high) , restData,1)
        restData = re.sub(r"#define SGOLD_VER_LOW.*0x[\dA-Fa-f]+", "#define SGOLD_VER_LOW 0x%02x" % string.atoi(self.config.sgold_ver_low) , restData,1)

        tmpf.write(restData)

        #reseek the position to file head, then change version id
        tmpf.seek(0, os.SEEK_SET)
        lines=tmpf.readlines()
        ix=0
        #this for loop is just used to change version id section
        for line in lines:
            #change version id
            if re.search(r"VERSION_ID.*"+self.type,line):
                #match below code,the verison id in Infiniti is different with DFL&NCIC&L42L
                #    2.5.14_20101009_BASELINE
                #    02.07R_20111025
                line=re.sub(r"\d+\..*\d",r'%s' % self.config.s12_ver_id, line)
                lines[ix] = line
            ix=ix+1
        tmpf.seek(0, os.SEEK_SET)
        tmpf.writelines(lines)

        f.close()
        tmpf.close()

        #write the new content to original file
        f = file(self.path+os.sep+CVfS12.name,'w')
        tmpf = file(self.path+os.sep+"bak_"+CVfS12.name)
        data=tmpf.read()
        f.write(data)

        #close all file
        f.close()
        tmpf.close()
        os.remove(self.path+os.sep+"bak_"+CVfS12.name)


def testNCIC():
    import config
    vf = CVfS12(r"D:\Temp\python\version", "NCIC")
    cfg = config.CConfig()

    vf.SetConfig(cfg)
    vf.Change()

def testDFL():
    import config
    vf = CVfS12(r"D:\Temp\python\version", "DFL")
    cfg = config.CConfig()

    vf.SetConfig(cfg)
    vf.Change()

def testInfiniti():
    import config
    vf = CVfS12(r"D:\Temp\python\version", "INFINITI")
    cfg = config.CConfig()

    vf.SetConfig(cfg)
    vf.Change()

def testL42L():
    import config
    vf = CVfS12(r"D:\Temp\python\version", "L42L")
    cfg = config.CConfig()
    vf.SetConfig(cfg)
    vf.Change()

def testSib():
    import config
    vf = CVfSgold(r"D:\Temp\python\version")
    cfg = config.CConfig()
    vf.SetConfig(cfg)
    vf.Change()
    

if __name__ == "__main__":
    testSib()
